function DateUtils() {
};

DateUtils.YEAR = 0;
DateUtils.MONTH = 1;
DateUtils.DAY = 2;
DateUtils.HOUR = 3;
DateUtils.MINUTE = 4;
DateUtils.SECOND = 5;
DateUtils.MILLISECOND = 6;

DateUtils.today = function() {
	return DateUtils.truncate(new Date(), DateUtils.DAY);
};

DateUtils.truncate = function(date, field) {
	var v = new Date(date.getTime());
	switch(field) {
		case DateUtils.YEAR: 
			v.setMonth(0);
		case DateUtils.MONTH: 
			v.setDate(1);
		case DateUtils.DAY: 
			v.setHours(0);
		case DateUtils.HOUR: 
			v.setMinutes(0);
		case DateUtils.MINUTE: 
			v.setSeconds(0);
		case DateUtils.SECOND: 
			v.setMilliseconds(0);
	}
	
	return v;
};

DateUtils.add = function (date, field, amount) {
	var v = new Date(date.getTime());
	switch(field) {
		case DateUtils.YEAR:
			v.setYear(v.getYear()+amount);
			break;
		case DateUtils.MONTH:
			v.setMonth(v.getMonth()+amount);
			break;
		case DateUtils.DAY:
			v.setDate(v.getDate()+amount);
			break;
		case DateUtils.HOUR:
			v.setHours(v.getHours()+amount);
			break;
		case DateUtils.MINUTE:
			v.setMinutes(v.getMinutes()+amount);
			break;
		case DateUtils.SECOND:
			v.setSeconds(v.getSeconds()+amount);
			break;
		case DateUtils.MILLISECOND:
			v.setMilliseconds(v.getMilliseconds()+amount);
			break;
	}
	
	return v;
};

DateUtils.isSameDay = function(d1, d2) {
	var _d1 = DateUtils.truncate(d1, DateUtils.DAY);
	var _d2 = DateUtils.truncate(d2, DateUtils.DAY);
	
	return _d1.getTime() == _d2.getTime();
};

DateUtils.isSameMoment = function(d1, d2, field) {
	var _d1 = DateUtils.truncate(d1, field);
	var _d2 = DateUtils.truncate(d2, field);
	
	return _d1.getTime() == _d2.getTime();
};

DateUtils.isSameInstant = function(d1, d2) {
	return d1.getTime() == d2.getTime();
};

DateUtils.daysDiff = function(d1, d2) {
	var d = DateUtils.truncate(d1, DateUtils.DAY).getTime() - DateUtils.truncate(d2, DateUtils.DAY).getTime() 
	return d / (24 * 60 * 60 * 1000);
};

DateUtils.diff = function(d1, d2, field) {
	var d = DateUtils.truncate(d1, field).getTime() - DateUtils.truncate(d2, field).getTime()
	var f = 1;
	 
	switch(field) {
		case DateUtils.YEAR: 
			d = d1.getYear() - d2.getYear();
			break;
			
		case DateUtils.MONTH: 
			d = (d1.getYear() - d2.getYear())*12 + d1.getMonth() - d2.getMonth();
			break;
			
		case DateUtils.DAY: 
			f = f*24;
		case DateUtils.HOUR: 
			f = f*60;
		case DateUtils.MINUTE: 
			f = f*60;
		case DateUtils.SECOND: 
			f = f*1000;
	}
	
	return d / f;
};

DateUtils.parse = function (fmt, s) {
	return $.datepicker.parseDate(fmt, s);
};

DateUtils.format = function(fmt, d) {
	return $.datepicker.formatDate(fmt, d);
};

